﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Timers;
using System.Diagnostics;
using System.ComponentModel;
using EasyStep.Data.Entities;
using EasyStep.Business.Interfaces;

namespace WPF.TestEditor
{

    public partial class ConstantPicker : MahApps.Metro.Controls.MetroWindow
    {
        public Action<TestConst> OnConstSelected;
        private ITestDataContext TestDataContext;
        private TestConst Data;
        public ConstantPicker(ITestDataContext testContext, string title, TestConst data, Action<TestConst> onConstSelected)
        {
            TestDataContext = testContext;
            OnConstSelected = onConstSelected;
            Data = data;
            InitializeComponent();
            this.Title = title;
            this.Loaded += new RoutedEventHandler(Window_Loaded);
            cmbConstName.SelectionChanged += new SelectionChangedEventHandler(cmbConstName_SelectionChanged);
        }

        void cmbConstName_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var item = GetSelected();
            if (item != null)
                txtValue.Text = item.Value;
        }

        void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var cData = Data;
            BindComboboxWithData();
            if (!SetSelected(cData))
                txtValue.Text = cData.Value;
            cmbConstName.Text = cData.Name;
        }

        private bool SetSelected(TestConst current)
        {
            var constExist = false;
            foreach (ComboBoxItem item in cmbConstName.Items)
            {
                var data = item.Tag as TestConst;
                if (data.Name.Equals(current.Name))
                {
                    item.IsSelected = true;
                    constExist = true;
                    break;
                }
            }
            return constExist;
        }
        private TestConst GetSelected()
        {
            var selitem = cmbConstName.SelectedItem as ComboBoxItem;
            if (selitem == null) return null;
            return selitem.Tag as TestConst;
        }


        private void BindComboboxWithData()
        {
            cmbConstName.Items.Clear();
            foreach (var item in TestDataContext.ConstantsManager.GetAll())
            {
                cmbConstName.Items.Add(new ComboBoxItem() { Content = item.Name, Tag = item });
            }
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            var name = cmbConstName.Text.Trim();
            Data = TestDataContext.ConstantsManager.Get(name);
            if (Data == null)
                Data = new TestConst()
                {
                    Name = name,
                    Value = txtValue.Text
                };
            else
                if (!Data.Value.Equals(txtValue.Text))
                {
                    Data.Value = txtValue.Text;
                    TestDataContext.ConstantsManager.Update(Data);
                }
            base.DialogResult = true;
            Close();
            OnConstSelected(Data);
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            base.DialogResult = false;
            base.Close();
        }
    }

}
